package com.ryskoo.customer.customeraccount.repository;

import com.ryskoo.common.constant.CacheConstant;
import com.ryskoo.customer.customeraccount.domain.CustomerAccountPO;
import com.ryskoo.framework.core.repository.BasicRedisImpl;
import com.ryskoo.framework.core.utils.RedisUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Primary;
import org.springframework.stereotype.Repository;

import javax.annotation.PostConstruct;
import java.util.List;

/**
 * @author hh
 */
@Primary
@Repository
public class CustomerAccountRedisImpl extends BasicRedisImpl<CustomerAccountPO> implements ICustomerAccountRepository {

	@Autowired
	private RedisUtil redisClient;

	@Autowired
	private ICustomerAccountRepository customerAccountRepository;

	@PostConstruct
	public void init() {
		this.setRedisClient(redisClient);
		this.setDao(customerAccountRepository);
	}

	public CustomerAccountRedisImpl() {
		super(CacheConstant.CUSTOMER_CUSTOMER_ACCOUNT_KEY);
	}

	@Override
	public void create(CustomerAccountPO m) {
		super.create(m);
		redisClient.setObject(CacheConstant.CUSTOMER_CUSTOMER_ACCOUNT_KEY + m.getCustomerUuid(), m);
	}

	@Override
	public void update(CustomerAccountPO m) {
		super.update(m);
		redisClient.setObject(CacheConstant.CUSTOMER_CUSTOMER_ACCOUNT_KEY + m.getCustomerUuid(), m);
	}

	@Override
	public void delete(CustomerAccountPO m) {
		super.delete(m);
		redisClient.remove(CacheConstant.CUSTOMER_CUSTOMER_ACCOUNT_KEY + m.getCustomerUuid());
	}

	@Override
	public void createOrUpdate(CustomerAccountPO m) {
		super.createOrUpdate(m);
		redisClient.setObject(CacheConstant.CUSTOMER_CUSTOMER_ACCOUNT_KEY + m.getCustomerUuid(), m);
	}

	/**
	 * 根据 customerUuids 查询customerAccount
	 * 
	 * @param customerUuids
	 * @return
	 */
	public List<CustomerAccountPO> getByCustomerUuids(List<String> customerUuids) {
		return customerAccountRepository.getByCustomerUuids(customerUuids);
	}

	/**
	 * 根据 customerUuid 查询customerAccount
	 * 
	 * @param customerUuid
	 * @return
	 */
	public CustomerAccountPO getByCustomerUuid(String customerUuid) {
		Object obj = redisClient.getObject(CacheConstant.CUSTOMER_CUSTOMER_ACCOUNT_KEY + customerUuid,
				CustomerAccountPO.class);
		if (null != obj) {
			return (CustomerAccountPO) obj;
		}
		CustomerAccountPO m = customerAccountRepository.getByCustomerUuid(customerUuid);
		if (null != m) {
			redisClient.setObject(CacheConstant.CUSTOMER_CUSTOMER_ACCOUNT_KEY + m.getCustomerUuid(), m);
		}
		return m;
	}

	/**
	 * 根据 customerUuids 删除 customerAccount
	 * 
	 * @param List<String>
	 *            needDeleteCustomerUuids
	 * @return
	 */
	public void deletesByCustomerUuids(List<String> needDeleteCustomerUuids) {
		customerAccountRepository.deletesByCustomerUuids(needDeleteCustomerUuids);
		// 删除缓存
		if (needDeleteCustomerUuids != null && needDeleteCustomerUuids.size() > 0) {
			for (String customerUuid : needDeleteCustomerUuids) {
				redisClient.remove(CacheConstant.CUSTOMER_CUSTOMER_ACCOUNT_KEY + customerUuid);
			}
		}
	}
}
